package com.dingtalk.h5app.quickstart.controller;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackRegisterCallBackRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.request.OapiProcessinstanceListidsRequest;
import com.dingtalk.api.response.OapiCallBackRegisterCallBackResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.dingtalk.api.response.OapiProcessinstanceListidsResponse;
import com.taobao.api.ApiException;

import java.util.Arrays;
import java.util.List;

public class TokenBySteven {

   static OapiGettokenResponse rsp;
        public static void main(String[] args) throws  Exception {
            try {
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
                OapiGettokenRequest req = new OapiGettokenRequest();
               /* req.setAppkey("dingeqkbzrfhtb5kglm7");
                req.setAppsecret("OoFM7y-JlgyI5MovVix0cjLe-7pdPiJfwpSzg7wGGh1F7rosqZa7wjOewFisIZHH");*/

                req.setAppkey("dingi2xjt0huri0syudd");
                req.setAppsecret("61-B5TKsSifLUwtA3De3Sz36xsYQzKBGX6qeoVMc48JeYG6yi-JzOP4r1cOF7XPm");
                req.setHttpMethod("GET");
                rsp = client.execute(req);

                System.out.println(rsp.getBody());

            } catch (ApiException e) {
                e.printStackTrace();
            }
            String process_code="PROC-BD305341-CC47-4349-9A51-F4BD44860F51";
            listids(process_code);
            callback();
        }

        public static void listids(String process_code) {
            String accessToken=rsp.getAccessToken();
            System.out.println("accessToken:"+accessToken);
            String url="https://oapi.dingtalk.com/topapi/processinstance/listids?access_token="+accessToken;
            try {
                DingTalkClient client = new DefaultDingTalkClient(url);
                OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
                req.setProcessCode(process_code);
                req.setStartTime(1496678400000L);
                OapiProcessinstanceListidsResponse rsp = client.execute(req, accessToken);
                List<String> stringList= rsp.getResult().getList();
                stringList.forEach(s->{
                    getInstance(s,accessToken);
                });
                System.out.println(rsp.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }
        }

        public static void getInstance(String process_instance_id,String accessToken ) {
            try {
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
                OapiProcessinstanceGetRequest req = new OapiProcessinstanceGetRequest();
                req.setProcessInstanceId(process_instance_id);
                OapiProcessinstanceGetResponse rsp = client.execute(req, accessToken);
                rsp.getProcessInstance().getFormComponentValues().forEach(item->{
                    System.out.println(item.getName()+":"+item.getValue()+"\r\n");
                });
                List<OapiProcessinstanceGetResponse.FormComponentValueVo> formComponentValueVos= rsp.getProcessInstance().getFormComponentValues();
                for(OapiProcessinstanceGetResponse.FormComponentValueVo formComponentValueVo :formComponentValueVos){
                    //如果是图片
                    if(formComponentValueVo.getComponentType().equalsIgnoreCase("DDPhotoField")){
                        //name要替换，value要替换
                        String value=formComponentValueVo.getValue();
                        //先替换掉所有的“\”，字符串处理
                        /*"name": "testCode(高度:200,宽度:300)",
                                "value": "[\"https:\/\/static.dingtalk.com\/media\/lAPPDeC2wkIeRzbNATvNAsE_705_315.bmp\"]"*/
                    }
                    //如果是表格
                    if(formComponentValueVo.getComponentType().equalsIgnoreCase("TableField")){
                        //"value": "[{\"rowValue\":[{\"componentType\":\"TextField\",\"label\":\"type\",\"value\":\"苹果\",\"key\":\"TextField-K8O7P4SU\"},{\"componentType\":\"TextField\",\"label\":\"presume\",\"value\":\"200\",\"key\":\"TextField-K8O7P4SV\"},{\"componentType\":\"TextField\",\"label\":\"thissum\",\"value\":\"2000\",\"key\":\"TextField-K8O7P4SW\"},{\"componentType\":\"DDPhotoField\",\"label\":\"testCode(高度:200,宽度:300)\",\"value\":\"[\\\"https:\/\/static.dingtalk.com\/media\/lAPPDeC2wkIeRzbNATvNAsE_705_315.bmp\\\"]\",\"key\":\"DDPhotoField-K8O7Q7MV\"}]},{\"rowValue\":[{\"componentType\":\"TextField\",\"label\":\"type\",\"value\":\"香蕉\",\"key\":\"TextField-K8O7P4SU\"},{\"componentType\":\"TextField\",\"label\":\"presume\",\"value\":\"100\",\"key\":\"TextField-K8O7P4SV\"},{\"componentType\":\"TextField\",\"label\":\"thissum\",\"value\":\"20\",\"key\":\"TextField-K8O7P4SW\"},{\"componentType\":\"DDPhotoField\",\"label\":\"testCode(高度:200,宽度:300)\",\"value\":\"[\\\"https:\/\/static.dingtalk.com\/media\/lALPDeREQObCcJ_MsMyw_176_176.png\\\"]\",\"key\":\"DDPhotoField-K8O7Q7MV\"}]}]"
                        //字符串处理，要把\ 替换为null

                    }
                }
                System.out.println(rsp.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

    }
    public static void callback() throws Exception{
            String accessToken="030bf972dd2631a8912344105883663b";
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/register_call_back");
        OapiCallBackRegisterCallBackRequest request = new OapiCallBackRegisterCallBackRequest();
        request.setUrl("http://106.13.36.159:8086/jeecg-boot/sys/getCheckCode");
        request.setAesKey("xxxxxxxxlvdhntotr3x9qhlbytb18zyz5zxxxxxxxxx");
        request.setToken("123456");
        request.setCallBackTag(Arrays.asList("user_add_org", "user_modify_org", "user_leave_org"));
        OapiCallBackRegisterCallBackResponse response = client.execute(request,accessToken);
        int x=0;
    }

    }

